package cn.lxs.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/ResponseDemo3")
public class ResponseDemo3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文问题
        /*resp.setCharacterEncoding("utf-8");
        * 这一行可以不用,因为下面浏览器会自动设置*/
        //在获取流之前来设置流的编码
        //告诉浏览器使用的编码
       /* resp.setHeader("content-type","text/html;charset=utf-8");
        *但还是复杂了点
        */
        resp.setContentType("text/html;charset=utf-8");
        //可以直接用这个

        //获取字符输出流
        PrintWriter pw = resp.getWriter();
        //输出数据
        pw.write("<h1>Hello response</h1>");
        pw.write("<h1>你好</h1>");
    }
}
